package org.example;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
	public static void main(String[] args) {
		String path = "C:\\ruanjian\\lookBook\\数据结构与算法\\胡玮薇\\student.json";
		try {
			// 解析 JSON
			JSONObject jsonObject = JSON.parseObject(Files.readString(Path.of(path)));
			JSONArray studentDataArray = jsonObject.getJSONArray("studentData");
			List<Student> students = studentDataArray.toJavaList(Student.class);
			quickSort(students, 0, students.size() - 1);
			mergeSort(students, 0, students.size() - 1);
			bubbleSort(students);
			selectionSort(students);
			insertionSort(students);
			insertStudent(students, new Student("aaaaa", 0, "New Student", "2000-01-01", true, "New Province", "New Class"));
			deleteStudent(students, "aaaaa");
			fuzzySearchByName(students, "张");
			countStudentsByProvince(students);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	// 快速排序
	public static void quickSort(List<Student> students, int low, int high) {
		while (true) {
			if (low < high) {
				int pivot = partition(students, low, high);
				quickSort(students, low, pivot - 1);
				low = pivot + 1;
				continue;
			}
			return;
		}
	}

	static int partition(List<Student> students, int low, int high) {
		Student pivot = students.get(high);
		int i = low - 1;
		for (int j = low; j < high; j++) {
			if (students.get(j).name.compareTo(pivot.name) < 0) {
				i++;
				Student temp = students.get(i);
				students.set(i, students.get(j));
				students.set(j, temp);
			}
		}
		Student temp = students.get(i + 1);
		students.set(i + 1, students.get(high));
		students.set(high, temp);
		return i + 1;
	}

	// 归并排序
	public static void mergeSort(List<Student> students, int low, int high) {
		if (low < high) {
			int mid = (low + high) / 2;
			mergeSort(students, low, mid);
			mergeSort(students, mid + 1, high);
			merge(students, low, mid, high);
		}
	}

	private static void merge(List<Student> students, int low, int mid, int high) {
		List<Student> temp = new ArrayList<>();
		int i = low, j = mid + 1;
		while (i <= mid && j <= high) {
			if (students.get(i).name.compareTo(students.get(j).name) <= 0) {
				temp.add(students.get(i++));
			} else {
				temp.add(students.get(j++));
			}
		}
		while (i <= mid) {
			temp.add(students.get(i++));
		}
		while (j <= high) {
			temp.add(students.get(j++));
		}
		for (int k = 0; k < temp.size(); k++) {
			students.set(low + k, temp.get(k));
		}
	}

	// 冒泡排序
	public static void bubbleSort(List<Student> students) {
		for (int i = 0; i < students.size() - 1; i++) {
			for (int j = 0; j < students.size() - i - 1; j++) {
				if (students.get(j).name.compareTo(students.get(j + 1).name) > 0) {
					Student temp = students.get(j);
					students.set(j, students.get(j + 1));
					students.set(j + 1, temp);
				}
			}
		}
	}

	// 选择排序
	public static void selectionSort(List<Student> students) {
		for (int i = 0; i < students.size() - 1; i++) {
			int minIndex = i;
			for (int j = i + 1; j < students.size(); j++) {
				if (students.get(j).name.compareTo(students.get(minIndex).name) < 0) {
					minIndex = j;
				}
			}
			Student temp = students.get(i);
			students.set(i, students.get(minIndex));
			students.set(minIndex, temp);
		}
	}

	// 插入排序
	public static void insertionSort(List<Student> students) {
		for (int i = 1; i < students.size(); i++) {
			Student key = students.get(i);
			int j = i - 1;
			while (j >= 0 && students.get(j).name.compareTo(key.name) > 0) {
				students.set(j + 1, students.get(j));
				j--;
			}
			students.set(j + 1, key);
		}
	}

	// 插入学生数据
	public static void insertStudent(List<Student> students, Student student) {
		students.add(student);
	}

	// 删除学生数据
	public static void deleteStudent(List<Student> students, String id) {
		for (int i = 0; i < students.size(); i++) {
			if (students.get(i).id.equals(id)) {
				students.remove(i);
				break;
			}
		}
	}

	// 名字模糊搜索
	public static void fuzzySearchByName(List<Student> students, String keyword) {
		List<Student> result = new ArrayList<>();
		for (Student student : students) {
			if (student.name.contains(keyword)) {
				result.add(student);
			}
		}
		for (Student student : result) {
			System.out.println(student.toString());
		}
	}

	// 排省份
	public static void countStudentsByProvince(List<Student> students) {
		Map<String, Integer> provinceCount = new HashMap<>();
		for (Student student : students) {
			provinceCount.put(student.provice, provinceCount.getOrDefault(student.provice, 0) + 1);
		}
		provinceCount.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue()));
	}
}

